﻿using DG.Tweening;
using Miao.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Miao.UI
{
    /// <summary>
    /// 从特定区域飞入
    /// </summary>
    public class PullUIMotion : BaseUIMotion
    {
        float duration;
        RectTransform.Edge edge;
        Vector3 fromPos, toPos;

        public override void Init()
        {
            base.Init();
            fromPos = toPos = TweenGameObject.transform.localPosition;
        }
        public PullUIMotion(RectTransform.Edge edge, float duration = 0.5f)
        {
            this.duration = duration;
            this.edge = edge;

        }
        public override void Play(bool forward, TweenCallback complete)
        {
            float width = Screen.width;
            float height = Screen.height;
            switch (edge)
            {
                case RectTransform.Edge.Left:
                    fromPos.x = forward ? -width : fromPos.x;
                    toPos.x = forward ? toPos.x : -width;
                    break;
                case RectTransform.Edge.Right:
                    fromPos.x = forward ? width : fromPos.x;
                    toPos.x = forward ? toPos.x : width;
                    break;
                case RectTransform.Edge.Top:
                    fromPos.y = forward ? height : fromPos.y;
                    toPos.y = forward ? toPos.y : height;
                    break;
                case RectTransform.Edge.Bottom:
                    fromPos.y = forward ? -height : fromPos.y;
                    toPos.y = forward ? toPos.y : -height;
                    break;
            }
            TweenGameObject.transform.localPosition = fromPos;
            Tweener tweener = TweenGameObject.transform.DOLocalMove(toPos, duration);
            tweener.SetEase(forward ? Ease.OutBack : Ease.InOutBack);
            tweener.OnComplete(complete);
        }
    }
}
